/**
 * Created by ddys on 15/10/6.
 */
define(['app'], function (app) {
    app.register.controller("TaskCtrl", ["$scope", "$rootScope", "$http", "$modal", '$stateParams', function ($scope, $rootScope, $http, $modal, $stateParams) {

        $rootScope.baseinfo.defaultMenuName = '订单服务';
        $rootScope.baseinfo.defaultSubMenuName = '任务列表';
        $scope.appId = $stateParams.appId;
        $scope.loadTaskList = function () {
            $http.post('task/tasklist.do', {'appId': $scope.appId})
                .success(function (data) {
                    $scope.taskList = data.data;
                    $scope.getTaskDetail($scope.taskList[0].baseTaskType);
                });
        }
        $scope.addTaskUI = function () {
            var scope = $rootScope.$new();
            scope.modal = {
                header: "增加任务",
                btnName: "保存",
                typeList: ['SLEEP', 'NOSLEEP'],
                processorType: 'SLEEP',
                heartBeatRate:5000,
                judgeDeadInterval :60000,
                sleepTimeNoData : 500,
                sleepTimeInterval:0,
                fetchDataNumber:500,
                executeNumber:1,
                threadNumber:5,
                maxTaskItemsOfOneThreadGroup:0,
                appId:$scope.appId
                }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/schedule/editTask.html',
                controller: "EditTaskCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }

        $scope.editTaskUI = function (baseTaskType,dealBeanName,heartBeatRate,judgeDeadInterval,
                                      threadNumber,processorType,fetchDataNumber,executeNumber,
                                      sleepTimeNoData,sleepTimeInterval,permitRunStartTime,permitRunEndTime,
                                      maxTaskItemsOfOneThreadGroup,taskParameter,taskItems) {
            var scope = $rootScope.$new();
            scope.modal = {
                header: "编辑任务",
                btnName: "保存",
                typeList: ['SLEEP', 'NOSLEEP'],
                baseTaskType:baseTaskType,
                dealBeanName:dealBeanName,
                processorType: processorType,
                heartBeatRate:heartBeatRate,
                judgeDeadInterval :judgeDeadInterval,
                sleepTimeNoData : sleepTimeNoData,
                sleepTimeInterval:sleepTimeInterval,
                fetchDataNumber:fetchDataNumber,
                executeNumber:executeNumber,
                threadNumber:threadNumber,
                permitRunStartTime:permitRunStartTime,
                permitRunEndTime:permitRunEndTime,
                taskParameter:taskParameter,
                taskItems:taskItems,
                maxTaskItemsOfOneThreadGroup:maxTaskItemsOfOneThreadGroup,
                appId:$scope.appId
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/schedule/editTask.html',
                controller: "EditTaskCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }

        $scope.addScheduleUI = function (taskName) {
            var scope = $rootScope.$new();
            scope.modal = {
                header: "增加策略",
                btnName: "保存",
                typeList: ['Schedule', 'Java', 'Bean'],
                type: 'Schedule',
                taskName: taskName,
                appId:$scope.appId
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/schedule/editSchedule.html',
                controller: "EditScheduleCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }
        $scope.editScheduleUI = function (strategyName, taskName, sts, kind, taskParameter, numOfSingleServer, assignNum, IPList) {
            var scope = $rootScope.$new();
            scope.modal = {
                header: "编辑策略",
                btnName: "更新",
                typeList: ['Schedule', 'Java', 'Bean'],
                kind: kind,
                taskName: taskName,
                strategyName: strategyName,
                sts: sts,
                taskParameter: taskParameter,
                numOfSingleServer: numOfSingleServer,
                assignNum: assignNum,
                ipList: IPList,
                appId:$scope.appId
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/schedule/editSchedule.html',
                controller: "EditScheduleCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.getTaskDetail(taskName);
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }
        $scope.delTaskUI = function (baseTaskType) {
            var scope = $rootScope.$new();
            scope.modal = {
                title: "删除任务",
                content: '你确定要删除' + baseTaskType + '吗?',
                baseTaskType: baseTaskType,
                appId: $scope.appId
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/modal/warning.html',
                controller: "DelTaskCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }
        $scope.delScheduleUI = function (strategyName, baseTaskType) {
            var scope = $rootScope.$new();
            scope.modal = {
                title: "删除策略",
                content: '你确定要删除' + strategyName + '吗?',
                taskName: baseTaskType,
                strategyName: strategyName,
                appId: $scope.appId
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/modal/warning.html',
                controller: "DelScheduleCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }
        $scope.backupTaskUI=function(){
            var scope = $rootScope.$new();
            scope.appId = $scope.appId;
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/schedule/backList.html',
                controller: "BackListCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadTaskList();
                }, function () {
                    $scope.loadTaskList();
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }

        $scope.clearTask=function(taskName){
            $http.post('task/clearTask.do', {'appId': $scope.appId, 'baseTaskType': taskName})
                .success(function (data) {
                    $scope.getTaskDetail(taskName);
                });
        }


        $scope.getTaskDetail = function (taskName) {
            $scope.taskName = taskName;
            $http.post('task/taskDetailList.do', {'appId': $scope.appId, 'taskName': taskName})
                .success(function (data) {
                    $scope.taskDetail = data.data;
                });
        }
        $scope.startOrStop = function (strategyName, taskName, sts) {
            var schedule = {};
            //TODO
            schedule.appId = $scope.appId;
            schedule.strategyName = strategyName;
            schedule.taskName = taskName;
            schedule.sts = sts;
            $http({
                method: 'POST',
                url: 'task/editTaskScheduleSts.do',
                data: schedule
            }).success(function (data) {
                $scope.getTaskDetail(taskName);
            });
        }
        $scope.loadTaskList();

    }]).controller("EditTaskCtrl", function ($scope, $modalInstance, $http) {

        $scope.ok = function () {
            var task = $scope.modal;
            $http({
                method: 'POST',
                url: 'task/editTask.do',
                data: task
            }).success(function (data) {
                $modalInstance.close();
            });

        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }).controller("EditScheduleCtrl", function ($scope, $modalInstance, $http) {

        $scope.ok = function () {
            var schedule = $scope.modal;
            $http({
                method: 'POST',
                url: 'task/editTaskSchedule.do',
                data: schedule
            }).success(function (data) {
                $modalInstance.close();
            });

        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }).controller("DelTaskCtrl", function ($scope, $modalInstance, $http) {

        $scope.ok = function () {
            var task = $scope.modal;
            $http({
                method: 'POST',
                url: 'task/delTask.do',
                data: task
            }).success(function (data) {
                $modalInstance.close();
            });

        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }).controller("DelScheduleCtrl", function ($scope, $modalInstance, $http) {

        $scope.ok = function () {
            var schedule = $scope.modal;
            $http({
                method: 'POST',
                url: 'task/delTaskSchedule.do',
                data: schedule
            }).success(function (data) {
                $modalInstance.close();
            });

        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }).controller("BackListCtrl", function ($scope, $modalInstance, $http) {
        var data = {"appId":$scope.appId}
        $scope.loadTaskList = function(){
            $http({
                method: 'POST',
                url: 'task/backupTaskList.do',
                data: data
            }).success(function (data) {
                $scope.data = data.data;
            });
        }
        $scope.backupTask=function(){
            $http({
                method: 'POST',
                url: 'task/backupTask.do',
                data: data
            }).success(function (data) {
                $scope.loadTaskList();
            });
        }

        $scope.delBackupTask=function(id){
            $http({
                method: 'POST',
                url: 'task/delBackupTask.do',
                data: {'id':id}
            }).success(function (data) {
                $scope.loadTaskList();
            });
        }

        $scope.recoveryBackupTask=function(id){
            $http({
                method: 'POST',
                url: 'task/recoveryBackupTask.do',
                data: {'id':id}
            }).success(function (data) {
                $modalInstance.dismiss('cancel');
            });
        }

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        $scope.loadTaskList();
    });

})